package com.echoplots.shardingjdbc.controller;

import java.util.Date;
import java.util.List;
import java.util.Random;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.echoplots.shardingjdbc.pojo.Order;
import com.echoplots.shardingjdbc.service.OrderService;
import com.google.common.collect.Lists;
@RestController
public class OrderController {

    @Resource
    private OrderService orderService;

    @GetMapping("/order")
    public String save(){
        List<String> merchantList = Lists.newArrayList("aliyun", "taobao", "tmall");

        for (int i = 0; i < 10; i++) {
            Order order = new Order();
            order.setUserId(i);
            order.setOrderNo(System.currentTimeMillis() + String.format("%06d", i));
            order.setOrderTime(new Date());
            order.setMerchant(merchantList.get(new Random().nextInt(merchantList.size())));
            orderService.save(order);

        }
        
        return "success";
    }
    
    
    @GetMapping("/select/{userId}")
    public Order select(@PathVariable Long userId){
    	Order order = orderService.selectByPrimaryKey(userId);
        return order;
    }
    
    
    @GetMapping("/select/merchant/{userId}")
    public Order selectMerchant(@PathVariable Integer userId){
    	Order or = new Order();
    	or.setUserId(userId);
    	or.setMerchant("taobao");
    	Order order = orderService.selectByPrimaryKeyAndMerchant(or);
        return order;
    }
    
    @GetMapping("/select/nocolmerchant/{userId}")
    public Order selectByPrimaryKeyAndNoColMerchant(@PathVariable Integer userId){
    	Order or = new Order();
    	or.setUserId(userId);
    	or.setMerchant("taobao");
    	Order order = orderService.selectByPrimaryKeyAndNoColMerchant(or);
    	return order;
    }
}
